package com.shopoo.oauth.app.assembler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.shopoo.oauth.app.converter.AppConverter;
import com.shopoo.oauth.domain.entity.Menu;
import com.shopoo.oauth.domain.entity.Role;
import com.shopoo.oauth.dto.clientobject.MenuCO;
import com.shopoo.oauth.dto.clientobject.MenuTree;
import com.shopoo.oauth.dto.clientobject.MenuTreeDTO;
import com.shopoo.oauth.dto.clientobject.MenuTreeResp;
import com.shopoo.oauth.dto.clientobject.MenuTreeSelect;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthMenu;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthRole;

/**
 * @Description:
 * @Auther: limy66
 * @Date: 2021/06/22/18:06
 */
public class AppAssembler {

    public static List<MenuCO> converterList(List<OauthMenu> menus) {
        List<MenuCO> list = new ArrayList<>();
        menus.forEach(oauthMenu -> {
            MenuCO menuCO = AppConverter.INSTANCE.toMenuCO(oauthMenu);
            list.add(menuCO);
        });
        return list;
    }

    public static List<MenuTreeDTO> transferMenuTreeDTO(List<Menu> list, Long pid) {
        List<MenuTreeDTO> menuTreeDTOList = new ArrayList<>();
        list.forEach(menu -> {
            MenuTreeDTO menuResp = AppConverter.INSTANCE.toMenuTreeDTO(menu);
            if (pid.equals(menu.getParentId())) {
                menuResp.setChildren(transferMenuTreeDTO(list, menu.getMenuId()));
                menuTreeDTOList.add(menuResp);
            }
        });
        return menuTreeDTOList;
    }

    public static List<MenuCO> transferMenuCOList(List<Menu> menus) {
        List<MenuCO> list = new ArrayList<>();
        menus.forEach(menu -> {
            MenuCO menuCO = AppConverter.INSTANCE.toMenuCO(menu);
            list.add(menuCO);
        });
        return list;
    }

    /**
     * @Description: 树形结构转换
     * @Author: limy66
     * @Date:   2021/5/12 16:13
     * @Param:  [list, pid]
     * @Return: java.util.List<com.bngrp.system.application.menu.dto.MenuTreeResp>
     */
    public static List<MenuTreeResp> transform(List<OauthMenu> list, Long pid) {
        List<MenuTreeResp> menuTreeResps = new ArrayList<>();
        Iterator<OauthMenu> iterator = list.iterator();
        while (iterator.hasNext()) {
            OauthMenu oauthMenu = iterator.next();
            MenuTreeResp menuTreeResp = AppConverter.INSTANCE.toMenuTreeResp(oauthMenu);
            if (pid.equals(oauthMenu.getParentId())) {
                menuTreeResp.setChildren(transform(list, oauthMenu.getMenuId()));
                menuTreeResps.add(menuTreeResp);
            }
        }
        return menuTreeResps;
    }

    /**
     * @Description: 属性结构转换
     * @Author: limy66
     * @Date:   2021/5/12 16:13
     * @Param:  [list, pid]
     * @Return: java.util.List<com.bngrp.system.application.menu.dto.MenuTreeSelect>
     */
    public static List<MenuTreeSelect> transformMenuTreeSelect(List<OauthMenu> list, Long pid) {
        List<MenuTreeSelect> menuTreeSelects = new ArrayList<>();
        Iterator<OauthMenu> iterator = list.iterator();
        while (iterator.hasNext()) {
            OauthMenu oauthMenu = iterator.next();
            MenuTreeSelect menuTreeSelect = AppConverter.INSTANCE.toMenuTreeSelect(oauthMenu);
            if (pid.equals(oauthMenu.getParentId())) {
                menuTreeSelect.setChildren(transformMenuTreeSelect(list, oauthMenu.getMenuId()));
                menuTreeSelects.add(menuTreeSelect);
            }
        }
        return menuTreeSelects;
    }

    public static List<MenuTree> transferMenuTree(List<Menu> list, Long pid) {
        List<MenuTree> menuTrees = new ArrayList<>();
        Iterator<Menu> iterator = list.iterator();
        while (iterator.hasNext()) {
            Menu menu = iterator.next();
            MenuTree menuTree = AppConverter.INSTANCE.toMenuTree(menu);
            if (pid.equals(menu.getParentId())) {
                menuTree.setChildren(transferMenuTree(list, menu.getMenuId()));
                menuTrees.add(menuTree);
            }
        }
        return menuTrees;
    }
}
